package com.efuture.androidmvpdemo.register

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.efuture.androidmvpdemo.databinding.ActivityRegisterBinding
import com.efuture.androidmvpdemo.login.LoginActivity
import com.efuture.androidmvpdemo.ui.base.BaseActivity
import com.efuture.androidmvpdemo.ui.register.RegisterInteractor
import com.efuture.androidmvpdemo.ui.register.RegisterPresenter
import com.efuture.androidmvpdemo.ui.register.RegisterView
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel

class RegitserActivity : BaseActivity(),RegisterView {
    val mainScope = MainScope()
    private lateinit var  binding: ActivityRegisterBinding
    private val persenter = RegisterPresenter(this, RegisterInteractor())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //关键代码
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        val rootView: View = binding!!.getRoot()
        setContentView(rootView)

        click()

    }

    private fun click() {

        binding?.btRegister.setOnClickListener {
            persenter.registerUser(mainScope,binding.etUsername.text.toString(),binding.etPassword.text.toString())
        }
    }

    override fun registerSuccess() {
        ToastUtils.showShort("注册成功")
        startActivity(Intent(this@RegitserActivity,LoginActivity::class.java))
    }

    override fun registerFail(reason:String) {

        ToastUtils.showShort(reason)

    }

    override fun clearInputData() {
        if(binding?.etUsername?.text?.length!! >0){
            binding?.etUsername?.setText("")
        }
        if(binding?.etPassword?.text?.length!! >0){
            binding?.etPassword?.setText("")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        persenter.onDestroyView(mainScope)


    }

}